var express = require('express');
var path = require('path');
var fs   = require("fs");
var router = express.Router();


router.get('/home', function(req, res, next) {
   pathwritehead('./views/index.html',req, res)
});

//登陆
router.get('/login', function(req, res, next) {
   pathwritehead('./views/login.html',req, res)
});

router.get('/IndustryStandard', function(req, res, next) {
   pathwritehead('./views/IndustryStandard/IndustryStandard.html',req, res)
});

router.get('/AddIndustryStandard', function(req, res, next) {
   pathwritehead('./views/AddIndustryStandard/AddIndustryStandard.html',req, res)
});


router.get('/JumpPage', function(req, res, next) {
   pathwritehead('./views/JumpPage/JumpPage.html',req, res)
});

//合作伙伴列表页
router.get('/CooperativePartner', function(req, res, next) {
   pathwritehead('./views/CooperativePartner/CooperativePartner.html',req, res)
});

//合作伙伴编辑页面
router.get('/AddNewPic', function(req, res, next) {
   pathwritehead('./views/AddNewPic/AddNewPic.html',req, res)
});
/*
* 说明：路径解析
* 参数：pathname:路径（项目中的绝对路径）
* 作者：王燕
* 日期：2014.6.9
* */
function pathwritehead(pathname,req, res) {
  //  var pathname = __dirname + url.parse(req.url).pathname;
    // pathname=__dirname+pathname;
    // if (path.extname(pathname) == "") {
    //     pathname += "/";
    // }
    fs.exists(pathname, function (exists) {
        if (exists) {
            switch (path.extname(pathname)) {
                case ".html":
                    res.writeHead(200, {"Content-Type": "text/html"});
                    break;
                case ".htm":
                    res.writeHead(200, {"Content-Type": "text/html"});
                    break;
                case ".js":
                    res.writeHead(200, {"Content-Type": "text/javascript"});
                    break;
                case ".css":
                    res.writeHead(200, {"Content-Type": "text/css"});
                    break;
                case ".gif":
                    res.writeHead(200, {"Content-Type": "image/gif"});
                    break;
                case ".jpg":
                    res.writeHead(200, {"Content-Type": "image/jpeg"});
                    break;
                case ".png":
                    res.writeHead(200, {"Content-Type": "image/png"});
                    break;
                default:
                    res.writeHead(200, {"Content-Type": "application/octet-stream"});
            }

            fs.readFile(pathname, function (err, data) {
                res.end(data);
            });
        } else {
            res.writeHead(404, {"Content-Type": "text/html"});
            res.end("<h1>404 Not Found</h1>");
        }
    });
}
module.exports = router;
